const Router = require('koa-router');
const ClassTimeRouter = new Router();
const ClassTimeService = require('../service/ClassTimeService');
const R = require('../utils/R');

ClassTimeRouter.prefix('/api/v1/classtime');

ClassTimeRouter.get('/list',async (ctx, next) => {
    let data = await ClassTimeService.findAll();
    if(data.name){
        ctx.body = R.errorMsg(data.name)
    }else {
        ctx.body = R.okData(data)
    }
})

ClassTimeRouter.post('/save', async (ctx, next) => {
    let obj = ctx.request.body;
    let data = await ClassTimeService.saveClassTime(obj);

    if(data.name){
        ctx.body = R.errorMsg(data.name)
    }else {
        ctx.body = R.okMsg('添加成功');
    }
})

ClassTimeRouter.post('/delete', async (ctx, next) => {
    let obj = ctx.request.body;
    let data = await ClassTimeService.deleteClassTime(obj.id);

    if(data.name || data == 0){
        ctx.body = R.errorMsg(data.name || '删除失败')
    }else {
        ctx.body = R.okMsg('删除成功')
    }
})

module.exports = ClassTimeRouter